package com.keshihao.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keshihao.eduservice.entity.EduVideo;
import com.keshihao.eduservice.feign.VodClient;
import com.keshihao.eduservice.mapper.EduVideoMapper;
import com.keshihao.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author keshihao
 * @since 2020-05-25
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    VodClient vodClient;

    @Override
    public void removeVideoByCourseId(String courseId) {

        List<EduVideo> eduVideoList = baseMapper.selectList(new QueryWrapper<EduVideo>().select("video_source_id").eq("course_id", courseId));

        List<String> videoIds = new ArrayList<>();
        // 将小节列表中的视频id取出来
        for (EduVideo eduVideo : eduVideoList) {
            String videoSourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)) {
                videoIds.add(videoSourceId);
            }

        }
        // 需要视频id列表不为空则进行删除
        if (videoIds.size() > 0) {
            vodClient.removeAlyVideos(videoIds);
        }

        baseMapper.delete(new QueryWrapper<EduVideo>().eq("course_id",courseId));

    }
}
